//do \\c3\rdat\SHoude\Research\EEgap\EEgap_scripts\Select_Available_Day_Pair_Match_vPaper_08072018.do 

global pathname="raw_data_folder"

use $pathname\lcidemo_046_2008_2012_allsales, clear

	drop if year==2007

	mvencode o_qty,mv(1) over
	sort pid
	merge pid using  $pathname\pairs_matched_2_size2only_manual_check_YQ_SH
	tab _m
	keep if _m==3
	drop _m
	collapse(sum) o_qty (median) retail promo,by(pid zipcode datenum)
	
	
//Identify week-zipcode where the products are available 	
	
	egen pid_zip=group(pid zipcode)

preserve
	collapse(sum) o_qty,by(pid zip pid_zip)
	ren 	o_qty sales_pid_zip
	sort pid_zip 
	save $pathname\pairs_matched_2_size2only_manual_check_YQ_SH_tmp, replace
restore	
	
	tsset pid_zip datenum
	tsfill
	
	mvencode o_qty,mv(0) over
	
	drop zip pid
	sort pid_zip
	merge pid_zip using $pathname\pairs_matched_2_size2only_manual_check_YQ_SH_tmp
	tab _m
	drop _m
	
	sort pid
	merge pid using  $pathname\pairs_matched_2_size2only_manual_check_YQ_SH
	tab _m
	keep if _m==3
	drop _m
	
	

	gen time_since_pid_zip=0
	sort pid zip datenum
	by pid  zipcode: replace time_since_pid_zip=time_since_pid_zip[_n-1]+1 if o_qty==0
	by pid  zipcode: egen  max_time_since=max(time_since_pid_zip)	 

	gen time_after_pid_zip=0
	gsort pid zip -datenum
	by pid zipcode: replace time_after_pid_zip=time_after_pid_zip[_n-1]+1 if o_qty==0
	
	gen avail_day_0_pid=0
	replace avail_day_0_pid=1 if o_qty>0

	gen avail_day_1_pid=0
	replace avail_day_1_pid=1 if time_since_pid_zip<=1 & time_after_pid_zip<=1 
	replace avail_day_1_pid=1 if o_qty>0
	//replace avail_day_1_pid=1 if Lag1_qty>0 & For1_qty>0  & Lag1_qty!=. & For1_qty!=.

	gen avail_day_2_pid=0
	replace avail_day_2_pid=1 if time_since_pid_zip<=2 & time_after_pid_zip<=2
	replace avail_day_2_pid=1 if o_qty>0
	//replace avail_day_2_pid=1 if (Lag1_qty>0 | Lag2_qty>0) &  (For1_qty>0 | For2_qty>0) & ((Lag1_qty!=. & For1_qty!=.) | (Lag2_qty!=. & For2_qty!=.) | (Lag1_qty!=. & For2_qty!=.) | (Lag2_qty!=. & For1_qty!=.)   )
	   
	gen avail_day_4_pid=0
	replace avail_day_4_pid=1 if time_since_pid_zip<=4 & time_after_pid_zip<=4
	replace avail_day_4_pid=1 if o_qty>0
	
	gen avail_day_6_pid=0
	replace avail_day_6_pid=1 if time_since_pid_zip<=6 & time_after_pid_zip<=6
	replace avail_day_6_pid=1 if o_qty>0
	
	gen avail_day_8_pid=0
	replace avail_day_8_pid=1 if time_since_pid_zip<=8 & time_after_pid_zip<=8
	replace avail_day_8_pid=1 if o_qty>0
	
	gen avail_day_12_pid=0
	replace avail_day_12_pid=1 if time_since_pid_zip<=12 & time_after_pid_zip<=12 
	replace avail_day_12_pid=1 if o_qty>0
	
	gen avail_day_24_pid=0
	replace avail_day_24_pid=1 if time_since_pid_zip<=24 & time_after_pid_zip<=24 
	replace avail_day_24_pid=1 if o_qty>0
	
	gen avail_day_36_pid=0
	replace avail_day_36_pid=1 if time_since_pid_zip<=36 & time_after_pid_zip<=36
	replace avail_day_36_pid=1 if o_qty>0
	
	gen avail_day_62_pid=0
	replace avail_day_62_pid=1 if time_since_pid_zip<=62 & time_after_pid_zip<=62
	replace avail_day_62_pid=1 if o_qty>0
	
	gen avail_day_92_pid=0
	replace avail_day_92_pid=1 if time_since_pid_zip<=92 & time_after_pid_zip<=92
	replace avail_day_92_pid=1 if o_qty>0
	
	gen avail_day_122_pid=0
	replace avail_day_122_pid=1 if time_since_pid_zip<=122 & time_after_pid_zip<=122
	replace avail_day_122_pid=1 if o_qty>0
	
	sort pid_pair_2_wmiss_kwh zipcode datenum	
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_0_pair_tmp=sum(avail_day_0_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_1_pair_tmp=sum(avail_day_1_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_2_pair_tmp=sum(avail_day_2_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_4_pair_tmp=sum(avail_day_4_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_6_pair_tmp=sum(avail_day_6_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_8_pair_tmp=sum(avail_day_8_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_12_pair_tmp=sum(avail_day_12_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_24_pair_tmp=sum(avail_day_24_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_36_pair_tmp=sum(avail_day_36_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_62_pair_tmp=sum(avail_day_62_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_92_pair_tmp=sum(avail_day_92_pid)
	by pid_pair_2_wmiss_kwh zipcode datenum: egen avail_day_122_pair_tmp=sum(avail_day_122_pid)
	
	tab  avail_day_0_pair_tmp
	tab  avail_day_1_pair_tmp
	tab  avail_day_2_pair_tmp
	tab  avail_day_4_pair_tmp
	tab  avail_day_6_pair_tmp
	tab  avail_day_8_pair_tmp
	tab  avail_day_12_pair_tmp
	tab  avail_day_24_pair_tmp
	tab  avail_day_36_pair_tmp
	tab  avail_day_62_pair_tmp
	tab  avail_day_92_pair_tmp
	tab  avail_day_122_pair_tmp
	
	
	gen avail_day_0_pair=0
	replace avail_day_0_pair=1 if  avail_day_0_pair_tmp==2
	
	gen avail_day_1_pair=0
	replace avail_day_1_pair=1 if  avail_day_1_pair_tmp==2
	
	gen avail_day_2_pair=0
	replace avail_day_2_pair=1 if  avail_day_2_pair_tmp==2
	
	gen avail_day_4_pair=0
	replace avail_day_4_pair=1 if  avail_day_4_pair_tmp==2
	
	gen avail_day_6_pair=0
	replace avail_day_6_pair=1 if  avail_day_6_pair_tmp==2
	
	gen avail_day_8_pair=0
	replace avail_day_8_pair=1 if  avail_day_8_pair_tmp==2
	
	gen avail_day_12_pair=0
	replace avail_day_12_pair=1 if  avail_day_12_pair_tmp==2
	
	gen avail_day_24_pair=0
	replace avail_day_24_pair=1 if  avail_day_24_pair_tmp==2
	
	gen avail_day_36_pair=0
	replace avail_day_36_pair=1 if  avail_day_36_pair_tmp==2
	
	gen avail_day_62_pair=0
	replace avail_day_62_pair=1 if  avail_day_62_pair_tmp==2
	
	gen avail_day_92_pair=0
	replace avail_day_92_pair=1 if  avail_day_92_pair_tmp==2
	
	gen avail_day_122_pair=0
	replace avail_day_122_pair=1 if  avail_day_122_pair_tmp==2
		
	keep pid_pair_2_wmiss_kwh zipcode datenum avail_day_0_pair avail_day_2_pair avail_day_1_pair avail_day_4_pair avail_day_6_pair avail_day_8_pair avail_day_12_pair avail_day_24_pair avail_day_36_pair  avail_day_62_pair avail_day_92_pair avail_day_122_pair 
	duplicates drop pid_pair_2_wmiss_kwh zipcode datenum avail_day_0_pair avail_day_2_pair avail_day_1_pair avail_day_4_pair avail_day_6_pair avail_day_8_pair avail_day_12_pair avail_day_24_pair avail_day_36_pair  avail_day_62_pair avail_day_92_pair avail_day_122_pair, force 
	sort pid_pair_2_wmiss_kwh zipcode datenum
save $pathname\Available_Day_pairs_matched_2_size2only_manual_check_YQ_SH, replace	
                             
tab avail_day_0_pair 	
tab avail_day_2_pair 
tab avail_day_1_pair 
tab avail_day_4_pair 
tab avail_day_6_pair 
tab avail_day_8_pair 
tab avail_day_12_pair 
tab avail_day_24_pair 
tab avail_day_36_pair 
tab avail_day_62_pair 
tab avail_day_92_pair 
tab avail_day_122_pair 
